﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LanuageInfo.Forms.Controls;
using LanguageInfo.BLL;
using DevExpress.XtraEditors;
using DevExpress.XtraTreeList.Nodes;

namespace LanuageInfo.Forms.CorporaInfo
{
    public partial class CorporaEditUc : UCBase
    {
        private DevExpress.XtraTreeList.Columns.TreeListColumn colVId;
        private DevExpress.XtraTreeList.Columns.TreeListColumn colVName;
        private DevExpress.XtraTreeList.Columns.TreeListColumn colVValue;

        public CorporaEditUc()
        {
            InitializeComponent();
            BindInitTree();
            AppendModelNodes(null);
        }

        public void BindInitTree()
        {
            this.colVId = new DevExpress.XtraTreeList.Columns.TreeListColumn();
            this.colVName = new DevExpress.XtraTreeList.Columns.TreeListColumn();
            colVValue = new DevExpress.XtraTreeList.Columns.TreeListColumn();

            this.treeListCorp.Columns.AddRange(new DevExpress.XtraTreeList.Columns.TreeListColumn[] {
            this.colVId,this.colVName,colVValue});

            this.colVId.Caption = "";
            this.colVId.FieldName = "Id";
            this.colVId.Name = "Id";
            
            this.colVName.OptionsColumn.AllowEdit = false;
            this.colVName.OptionsColumn.AllowSort = false;
            this.colVName.Caption = "名称";
            this.colVName.FieldName = "Name";
            this.colVName.Name = "Name";
            this.colVName.VisibleIndex = 0;

            this.colVValue.Caption = "内容";
            this.colVValue.FieldName = "Value";
            this.colVValue.Name = "Value";
            this.colVValue.OptionsColumn.AllowEdit = true;
            this.colVValue.OptionsColumn.AllowSort = false;
            this.colVValue.VisibleIndex = 5;
        }
        WordsBLL objWll = new WordsBLL();
        private void BindLbc(string strInfo)
        {
            LbcSeach.DataSource = null;
            DataSet ds = objWll.GetWordsInfoTopFrm(strInfo);
            if (ds != null && ds.Tables.Count > 0)
            {
                LbcSeach.DataSource = ds.Tables[0];
                LbcSeach.DisplayMember = "HEADWORD";
                LbcSeach.ValueMember = "WORDSID";
            }
        }

        private void AppendModelNodes(TreeListNode aNode)
        {
            Cursor currentCursor = Cursor.Current;
            Cursor.Current = Cursors.WaitCursor;
            treeListCorp.BeginUnboundLoad();
            TreeListNode node;
            string strId= string.Empty;
            if (aNode == null)
            {
                strId = "0";
            }
            else
            {
              strId = ((object[])aNode.Tag)[0].ToString();
            }
            string strState = string.Empty;
            DataSet ds = objWll.WordsInfoTreeById(strId);
            if(ds!=null&&ds.Tables.Count>0)
            {
            foreach (DataRow s in ds.Tables[0].Rows)
            {
                try
                {
                    node = treeListCorp.AppendNode(new object[] {s["id"].ToString(),s["name"].ToString(),""}, aNode);
                    node.HasChildren = Convert.ToInt32(s["state"].ToString()) > 0;
                    node.Tag = new object[] { s["id"].ToString(), true };
                }
                catch { }
            }
            }
            treeListCorp.EndUnboundLoad();
            Cursor.Current = currentCursor;
        }

        private void sBtSeach_Click(object sender, EventArgs e)
        {
            BindLbc(cbeSeach.Text.Trim());
        }

        private void cbeSeach_EditValueChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(cbeSeach.Text.Trim()))
            {
                cbeSeach.ClosePopup();
                cbeSeach.EditValue = "";
            }

            if (!cbeSeach.IsPopupOpen)
            {
                string strSeachContent = cbeSeach.Text.Trim();
                cbeSeach.Properties.Items.Clear();
                DataSet _ds = objWll.GetWordsInfoTopFrm(strSeachContent);
                if (_ds.Tables.Count > 0)
                {
                    for (int i = 0; i < _ds.Tables[0].Rows.Count; i++)
                    {
                        cbeSeach.Properties.Items.Add(_ds.Tables[0].Rows[i]["HEADWORD"].ToString());
                    }

                }
            }
            if (cbeSeach.Properties.Items.Count > 0)
            {
                cbeSeach.ShowPopup();
            }
        }

        private void cbeSeach_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                sBtSeach_Click(sBtSeach, null);
            }
        }

        private void LbcSeach_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (LbcSeach.SelectedItem == null)
            {
                return;
            }
            string strWorldId = ((System.Data.DataRowView)LbcSeach.SelectedItem).Row["WORDSID"].ToString();
            //string strbb = LbcSeach.SelectedItem.ToString();
        }

        private void treeListCorp_BeforeExpand(object sender, DevExpress.XtraTreeList.BeforeExpandEventArgs e)
        {
            if (e.Node.Tag != null)
            {
                AppendModelNodes(e.Node);
                //object[] obj = (object[])e.Node.Tag;
                //if (Convert.ToBoolean(obj[1]))
                //{
                //    IList<ViewETLTaskAndTaskGroup> list = new List<ViewETLTaskAndTaskGroup>();
                //    list = _ServiceProxy.SelectViewETLTaskAndTaskGroupByPId(condition, conditionTime, obj[0].ToString(), objAl.ToArray());
                //    rootVList = new List<ViewETLTaskAndTaskGroup>();
                //    rootVList = list;
                //    AppendVNodes(e.Node);
                //    e.Node.Tag = new object[] { obj[0], false };
                //}
            }
        }

        
    }

   
}
